﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.Admin
{
    public partial class AdminManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {
            AdminsBusiness ab = new AdminsBusiness();
            DataTable dt = ab.GetAllData();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            AdminsEntity ae = new AdminsEntity();
            ae.Account = TxtAccount.Text;
            ae.Password = TxtPassword.Text;
            ae.Telephone = TxtTelephone.Text;

            AdminsBusiness ab = new AdminsBusiness();
            int res = ab.AddAdmin(ae);
            if (res > 0)
            {
                Response.Write("<script>alert('添加成功！')</script>");
                BindData();
            }
            else
            {
                Response.Write("<script>alert('添加失败！')</script>");
            }
        }

        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            if (TxtAdminid.Text != "")
            {
                AdminsEntity ae = new AdminsEntity();
                ae.Adminid = Convert.ToInt32(TxtAdminid.Text);
                ae.Account = TxtAccount.Text;
                ae.Password = TxtPassword.Text;
                ae.Telephone = TxtTelephone.Text;

                AdminsBusiness ab = new AdminsBusiness();
                int res = ab.EditAdmin(ae);
                if (res > 0)
                {
                    Response.Write("<script>alert('更新成功！')</script>");
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('更新失败！')</script>");
                }

            }
            else
            {
                Response.Write("<script>alert('请选择需要修改的商品！')</script>");
            }
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            AdminsEntity ae = new AdminsEntity();
            ae.Account = TxtSearch.Text;

            AdminsBusiness ab = new AdminsBusiness();
            DataTable dt = ab.SearchAllData(ae);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "删除")
            {
                AdminsEntity ae = new AdminsEntity();
                ae.Adminid = Convert.ToInt32(e.CommandArgument);

                AdminsBusiness ab = new AdminsBusiness();
                int res = ab.DeleteAdmin(ae);
                if (res > 0)
                {
                    Response.Write("<script>alert('删除成功！')</script>");
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('删除失败！')</script>");
                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectIndex = GridView1.SelectedIndex;
            if (selectIndex != -1)
            {
                TxtAdminid.Text = GridView1.Rows[selectIndex].Cells[1].Text;
                TxtAccount.Text = GridView1.Rows[selectIndex].Cells[2].Text;
                TxtPassword.Text = GridView1.Rows[selectIndex].Cells[3].Text;
                TxtTelephone.Text = GridView1.Rows[selectIndex].Cells[4].Text;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {

        }

        private void SelectPage(int pageNum)
        {
            AdminsBusiness ab = new AdminsBusiness();

            DataTable dt = ab.GetPageData(pageNum);
            if (dt != null && dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;

            SelectPage(e.NewPageIndex);
        }
    }
}